package com.linkage.educloud.ah.adapter.dynamic;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Looper;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.DynamicBean;
import com.linkage.educloud.ah.bean.DynamicImageBean;
import com.linkage.educloud.ah.bean.PicItem;
import com.linkage.educloud.ah.util.ImageLoader;
import com.linkage.educloud.ah.view.activity.big_picture.BigPictureActivity;
import com.linkage.educloud.ah.view.activity.classspace.DynamicDetailActivity;
import com.linkage.educloud.ah.widget.ExpressionUtils;
import com.zzhou.common.library.util.ToastShow;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Mr'Tang on 2018/9/17.
 */

public class DynamicListAdapter extends RecyclerView.Adapter<DynamicListAdapter.ViewHolder> {
    private List<DynamicBean> mdicList = new ArrayList<DynamicBean>();
    private DynamicBean dynamic;
    private Activity mcontext;
    private String clazz_id;
    private int voice_isplay = 0;//语音是否播放   0为否

    private OnpraiseClickListener onpraiseClickListener;
    private OncommentClickListener oncommentClickListener;

    public DynamicListAdapter(Activity context, List<DynamicBean> dicList, String class_id) {
        this.mcontext = context;
        this.mdicList = dicList;
        this.clazz_id = class_id;
    }

    public void addAll(List<DynamicBean> data, boolean append) {
        if (mdicList != null) {
            if (!append) {
                mdicList.clear();
            }
            mdicList.addAll(data);
        } else {
            mdicList = data;
        }
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_dynamiclist, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public int getItemCount() {
        return mdicList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
        dynamic = mdicList.get(position);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DynamicDetailActivity.launch(mcontext, mdicList, position, clazz_id);
            }
        });

        ImageLoader.loadAvatar(mcontext, Constants.IMAGE_IP_common + dynamic.getAvatar(), viewHolder.cs_avatar);
        viewHolder.cs_time.setText(dynamic.getTime());//TimeUtils.getStandardDate(dynamic.getTime())
        viewHolder.cs_name.setText(dynamic.getName());
        if (dynamic.getRole() == 1) {
            viewHolder.cs_role_person.setVisibility(View.VISIBLE);
            viewHolder.cs_role_teacher.setVisibility(View.VISIBLE);
        } else {
            viewHolder.cs_role_person.setVisibility(View.GONE);
            viewHolder.cs_role_teacher.setVisibility(View.GONE);
        }

        if (dynamic.getContent_type() == 1) {
            viewHolder.cs_rel_content.setVisibility(View.VISIBLE);
            viewHolder.cs_rel_image.setVisibility(View.GONE);
            viewHolder.cs_rel_voice.setVisibility(View.GONE);
            viewHolder.cs_content_text.setText(ExpressionUtils.replaceFace(mcontext,
                    dynamic.getContent()));
            viewHolder.cs_image_intrude.setVisibility(View.GONE);
        } else if (dynamic.getContent_type() == 2) {
            viewHolder.cs_rel_content.setVisibility(View.VISIBLE);
            viewHolder.cs_rel_image.setVisibility(View.VISIBLE);
            viewHolder.cs_rel_voice.setVisibility(View.GONE);
            viewHolder.cs_content_text.setText(ExpressionUtils.replaceFace(mcontext,
                    dynamic.getContent()));
            if (dynamic.getType() == 2 || dynamic.getType() == 4
                    || dynamic.getType() == 5) {
                viewHolder.cs_image_intrude.setVisibility(View.VISIBLE);
                viewHolder.cs_image_intrude.setText("上传了" + dynamic.getImageList().size()
                        + "张照片到《班级相册》");
            } else {
                viewHolder.cs_image_intrude.setVisibility(View.VISIBLE);
                viewHolder.cs_image_intrude.setText("共传了" + dynamic.getImageList().size()
                        + "张照片");
            }

            final ArrayList<PicItem> picList = new ArrayList<PicItem>();
            for (int i = 0; i < dynamic.getImageList().size(); i++) {
                DynamicImageBean image = dynamic.getImageList().get(i);
                String uri = image.getImage().replace(
                        "small", "big");
                PicItem it;
                if (uri.startsWith("http")) {
                    it = new PicItem(uri, i);
                } else {
                    if (dynamic.getType() == 4) {
                        it = new PicItem(
                                Constants.IMAGE_IP_hexiaoxiao + uri, i);
                    } else {
                        it = new PicItem(
                                Constants.IMAGE_IP_common
                                        + uri, i);
                    }
                }
                picList.add(it);
            }

            if (dynamic.getImageList().size() == 1) {
                viewHolder.cs_image_oneview.setVisibility(View.VISIBLE);
                viewHolder.cs_image_gridview.setVisibility(View.GONE);
                final String uri = dynamic.getImageList().get(0).getImage()
                        .replace("small", "big");
                if (uri.startsWith("http")) {
                    ImageLoader.loadImg(mcontext, uri, viewHolder.cs_image_oneview);
                } else {
                    if (dynamic.getType() == 4) {
                        ImageLoader.loadImg(mcontext, Constants.IMAGE_IP_hexiaoxiao + uri, viewHolder.cs_image_oneview);
                    } else {
                        ImageLoader.loadImg(mcontext, Constants.IMAGE_IP_common + uri, viewHolder.cs_image_oneview);
                    }
                }
                viewHolder.cs_image_oneview.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BigPictureActivity.launch(mcontext, picList, 0);
                    }
                });
            } else {
                viewHolder.cs_image_oneview.setVisibility(View.GONE);
                viewHolder.cs_image_gridview.setVisibility(View.VISIBLE);
                viewHolder.cs_image_gridview.setSelector(new ColorDrawable(
                        Color.TRANSPARENT));
                viewHolder.cs_image_gridview.setAdapter(new DynamicImageListAdapter(mcontext, dynamic
                        .getImageList()));
            }
            viewHolder.cs_image_gridview
                    .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent,
                                                View view, int pos, long id) {
                            BigPictureActivity.launch(mcontext, picList, pos);
                        }
                    });
        } else if (dynamic.getContent_type() == 3) {
            viewHolder.cs_rel_content.setVisibility(View.GONE);
            viewHolder.cs_rel_image.setVisibility(View.GONE);
            viewHolder.cs_rel_voice.setVisibility(View.VISIBLE);
            viewHolder.cs_image_intrude.setVisibility(View.GONE);
            final String videoUrl = Constants.IMAGE_IP_common
                    + dynamic.getContent();
            //语音播放
            viewHolder.cs_voice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    new Thread(new Runnable() {
//                        @Override
//                        public void run() {
//                            Looper.prepare();
                    play_voice(videoUrl, viewHolder.cs_voice);
//                            Looper.loop();
//                        }
//                    }).start();
                }
            });
        }
        //评论
        viewHolder.cs_comment_num.setText(dynamic.getCommentnum() + "");
        if (dynamic.getCommentnum() != 0) {
            final DynamicCommentAdapter commentAdapter = new DynamicCommentAdapter(mcontext,
                    dynamic.getCommentList(), 1);
            viewHolder.cs_comment_list.setVisibility(View.VISIBLE);
            viewHolder.cs_comment_list.setAdapter(commentAdapter);
            if (dynamic.getCommentnum() > 5) {
                viewHolder.cs_lin_allcomment.setVisibility(View.VISIBLE);
            } else {
                viewHolder.cs_lin_allcomment.setVisibility(View.GONE);
            }
        } else {
            viewHolder.cs_comment_list.setVisibility(View.GONE);
        }

        //评论接口
        viewHolder.cs_lin_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                oncommentClickListener.onItemClick(position);
            }
        });
        //查看全部评论  点击进入详情页
        viewHolder.cs_lin_allcomment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DynamicDetailActivity.launch(mcontext, mdicList, position, clazz_id);
            }
        });

        //点赞
        viewHolder.cs_priased_num.setText(dynamic.getPraisednum() + "");
        if (dynamic.getIspriased() == 0) {
            viewHolder.cs_priased.setImageResource(R.mipmap.ic_praised_on);
        } else {
            viewHolder.cs_priased.setImageResource(R.mipmap.ic_praised_un);
        }
        viewHolder.cs_priased_text.setText(Html.fromHtml(flushPriase(dynamic)));
        //点赞接口
        viewHolder.cs_lin_praised.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onpraiseClickListener.onItemClick(position);
            }
        });
    }

    //  删除数据
    public void removeData(int position) {
        mdicList.remove(position);
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public CircleImageView cs_avatar;//圆形头像
        public TextView cs_name;//名字
        public ImageView cs_role_person, cs_role_teacher;//教师头衔
        public TextView cs_time;//时间
        public RelativeLayout cs_rel_content;//文字布局
        public TextView cs_content_text;//文字
        public RelativeLayout cs_rel_image;//图片布局
        public ImageView cs_image_oneview;//只有一张图片
        public GridView cs_image_gridview;//图片gridview
        public RelativeLayout cs_rel_voice;//声音布局
        public ImageView cs_voice;//声音图标
        public TextView cs_image_intrude;//图片介绍
        public LinearLayout cs_lin_comment;//评论布局
        public TextView cs_comment_num;//评论数量
        public LinearLayout cs_lin_praised;//赞布局
        public ImageView cs_priased;//赞图标
        public TextView cs_priased_num;//赞数量
        public TextView cs_priased_text;//赞的具体人
        public ListView cs_comment_list;//评论具体listview
        public LinearLayout cs_lin_allcomment;//查看全部评论

        public ViewHolder(View view) {
            super(view);
            cs_avatar = view.findViewById(R.id.cs_avatar);
            cs_name = view.findViewById(R.id.cs_name);
            cs_role_person = view.findViewById(R.id.cs_role_person);
            cs_role_teacher = view.findViewById(R.id.cs_role_teacher);
            cs_time = view.findViewById(R.id.cs_time);
            cs_rel_content = view.findViewById(R.id.cs_rel_content);
            cs_content_text = view.findViewById(R.id.cs_content_text);
            cs_rel_image = view.findViewById(R.id.cs_rel_image);
            cs_image_oneview = view.findViewById(R.id.cs_image_oneview);
            cs_image_gridview = view.findViewById(R.id.cs_image_gridview);
            cs_rel_voice = view.findViewById(R.id.cs_rel_voice);
            cs_voice = view.findViewById(R.id.cs_voice);
            cs_image_intrude = view.findViewById(R.id.cs_image_intrude);
            cs_lin_comment = view.findViewById(R.id.cs_lin_comment);
            cs_comment_num = view.findViewById(R.id.cs_comment_num);
            cs_lin_praised = view.findViewById(R.id.cs_lin_praised);
            cs_priased = view.findViewById(R.id.cs_priased);
            cs_priased_num = view.findViewById(R.id.cs_priased_num);
            cs_priased_text = view.findViewById(R.id.cs_priased_text);
            cs_comment_list = view.findViewById(R.id.cs_comment_list);
            cs_lin_allcomment = view.findViewById(R.id.cs_lin_allcomment);
        }
    }

    private void play_voice(String url, final ImageView img) {
        final MediaPlayer mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(url);
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }


//        final MediaPlayer mediaPlayer = new MediaPlayer();
//        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//        Uri uri = Uri.parse(url);
//        try {
//            mediaPlayer.setDataSource(mcontext, uri);
//            mediaPlayer.prepare();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
//            @Override
//            public void onCompletion(MediaPlayer mp) {
//                voice_isplay = 0;
//                img.setImageResource(R.mipmap.voice_play);
//                mediaPlayer.stop();
//            }
//        });
//        if (voice_isplay == 0) {
//            voice_isplay = 1;
//            img.setImageResource(R.mipmap.voice_off);
//            mediaPlayer.start();
//        } else if (mediaPlayer.isPlaying() && voice_isplay == 1) {
//            voice_isplay = 0;
//            img.setImageResource(R.mipmap.voice_play);
//            mediaPlayer.stop();
//        }
    }

    //加载 点赞 数据样式
    private static String flushPriase(DynamicBean mic) {
        if (mic.getPraisednum() < 10 && mic.getPraisednum() > 0) {
            StringBuffer bf = new StringBuffer();
            for (int a = 0; a < mic.getPriasedList().size(); a++) {
                if (a == mic.getPriasedList().size() - 1) {
                    bf.append(mic.getPriasedList().get(a).getUser_name());
                } else {
                    bf.append(mic.getPriasedList().get(a).getUser_name() + "、");
                }
            }
            return "<font color='#ff0D92DD'>" + bf.toString() + "</font>" + "觉得赞";
        } else if (mic.getPraisednum() >= 10) {
            StringBuffer bf = new StringBuffer();
            for (int a = 0; a < 10; a++) {
                if (a == 9) {
                    bf.append(mic.getPriasedList().get(a).getUser_name());
                } else {
                    bf.append(mic.getPriasedList().get(a).getUser_name() + "、");
                }
            }
            return "<font color='#ff0D92DD'>" + bf.toString() + "</font>" + "等" + mic.getPraisednum() + "人觉得赞";
        } else {
            return "0人觉得赞";
        }
    }

    //点赞回调接口
    public interface OnpraiseClickListener {
        void onItemClick(int position);
    }

    public void setOnpraiseClickListener(OnpraiseClickListener onItemActionClick) {
        this.onpraiseClickListener = onItemActionClick;
    }

    //评论回调接口
    public interface OncommentClickListener {
        void onItemClick(int position);
    }

    public void setOncommentClickListener(OncommentClickListener onItemActionClick) {
        this.oncommentClickListener = onItemActionClick;
    }
}
